﻿using Microsoft.Extensions.DependencyInjection;

namespace DI会传染
{
  internal class Program
  {
    static void Main(string[] args)
    {
      ServiceCollection services = new ServiceCollection();
      services.AddScoped<Controller>();
      services.AddScoped<ILog, LogImp1>();
      services.AddScoped<IStorage, StorageImpl>();
      services.AddScoped<IConfig, ConfigImpl>();
      using (var sp=services.BuildServiceProvider())
      {
        var c = sp.GetRequiredService<Controller>();
        c.Test();
      }
      Console.ReadLine();
    }

  }
  class Controller
  {
    private readonly ILog log;
    private readonly IStorage storage;

    public Controller(ILog log, IStorage storage)
    {
      this.log = log;
      this.storage = storage;
    }
    public void Test()
    {
      log.Log("开始上传");
      storage.Save("dkkk", "1.txt");
      log.Log("上传完成");
    }
  }
  interface ILog
  {
    public void Log(string message);
  }
  class LogImp1 : ILog
  {
    public void Log(string message)
    {
      Console.WriteLine($"日志：{message}");
    }
  }
  interface IConfig
  {
    string GetValue(string name);
  }
  class ConfigImpl : IConfig
  {
    public string GetValue(string name)
    {
      return "hello";
    }
  }

  interface IStorage
  {
    void Save(string content, string name);
  }
  class StorageImpl : IStorage
  {
    private readonly IConfig config;
    public StorageImpl(IConfig config)
    {
      this.config = config;
    }
    public void Save(string content, string name)
    {
      string server = config.GetValue("server");
      Console.WriteLine($"向服务器{server}的文件名为{name}上传{content}");
    }
  }





}